package com.augurit.gzcgwsp.lhsp.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.SQLQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.util.PageUtils;
import com.augurit.gzcgwsp.lhsp.convert.LhspUpdateFlowStateConvert;
import com.augurit.gzcgwsp.lhsp.dao.LhspUpdateFlowStateDao;
import com.augurit.gzcgwsp.lhsp.entity.LhspUpdateFlowState;
import com.augurit.gzcgwsp.lhsp.service.ILhspUpdateFlowStateService;
import com.augurit.gzcgwsp.lhsp.web.form.LhspUpdateFlowStateForm;
import com.augurit.gzcgwsp.printDoc.service.IYwPrintDocService;
import com.augurit.gzcgwsp.wfex.common.util.WfUtilsEX;
import com.augurit.wf.common.service.impl.WfBusClientServiceImpl;

/**
 * 接口四：更新办理状态接口  把数据传到并联审批系统
 * @author adm
 *
 */
@Service
@Transactional
public class LhspUpdateFlowStateServiceImpl extends WfBusClientServiceImpl<LhspUpdateFlowStateForm>  implements ILhspUpdateFlowStateService {
	private static final Logger log=Logger.getLogger(LhspUpdateFlowStateServiceImpl.class);
	@Autowired
	private LhspUpdateFlowStateDao lhspSendCommandNoticeDao;
	@Autowired
	private IYwPrintDocService ywPrintDocService;
	
	/**
	 * 保存数据到数据库
	 * @param templateCode
	 * @param acceptNo 
	 * @param projectId 工程ID
	 * @param depId 部门编号，详见部门编号对照表
	 * @param state 状态，固定值[受理、办结]汉字
	 * @param Opinion  办理意见(2000字内)
	 * @param OpinionResult 办理结果，包括 [同意、不同意、一致、不一致、涉及、不涉及]
	 * @return 
	 */
	public String saveLhspUpdateFlowState(String templateCode,String acceptNo,String projectId,String depId,String state,String opinion,String opinionResult) throws Exception{
		LhspUpdateFlowState lhspUpdateFlowState = new LhspUpdateFlowState();
		lhspUpdateFlowState.setTemplateCode(templateCode);
		lhspUpdateFlowState.setAcceptNo(acceptNo);
		lhspUpdateFlowState.setProjectId(projectId);
		lhspUpdateFlowState.setDepId(depId);
		lhspUpdateFlowState.setState(state);
		lhspUpdateFlowState.setOpinion(opinion);
		lhspUpdateFlowState.setOpinionResult(opinionResult);
		lhspUpdateFlowState.setProduceDate(new Date());
		this.save(lhspUpdateFlowState);
		return null;
	}
	
	@Override
	public Page<LhspUpdateFlowStateForm> search(Page<LhspUpdateFlowStateForm> page,
			List<PropertyFilter> filters) {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * 查询
	 * @param page 
	 * @param form 
	 * @return page
	 */
	public Page<LhspUpdateFlowStateForm> search(Page<LhspUpdateFlowStateForm> page,
			LhspUpdateFlowStateForm form) {
		Page pg = PageUtils.newPageInstance(page);
		StringBuffer hql = new StringBuffer("from LhspSendCommandNotice where 1=1");
		List values = new ArrayList();
		lhspSendCommandNoticeDao.findPage(pg, hql.toString(), values);		
		List<LhspUpdateFlowStateForm> list = LhspUpdateFlowStateConvert.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	
	}
	
	/**
	 * 根据受理号和业务编码获取工程projectId
	 * @param acceptNo
	 * @param templateCode
	 * @return
	 */
	public String getProjectIdByAcceptNoAndTemplateCode(String acceptNo,String templateCode) throws Exception{
		String returnStr = "";
		if("csdlwj".equals(templateCode) || "csdllszysp".equals(templateCode) || templateCode.contains("jzfqwczzxn_") || templateCode.contains("jzfqwczzpf_")){
			StringBuffer sql = new StringBuffer();
			sql.append("select t.project_id from LHSP_SEND_ITEM_NOTICE t,");
			if("csdlwj".equals(templateCode)){
				sql.append(" SR_DLWJSQB_V4 a ");
			}else if("csdllszysp".equals(templateCode)){
				sql.append(" SR_DLZYSQB_V1 a ");
			}else if(templateCode.contains("jzfqwczzxn_")){
				sql.append(" YW_YN_JZFQWCZC_XN a ");
			}else if(templateCode.contains("jzfqwczzpf_")){
				sql.append("YW_YN_JZFQWCZC_PFSN a ");
			}
			sql.append(" where a.lhsp_id is not null and t.id=a.lhsp_id and a.accept_no='"+acceptNo+"'");
			SQLQuery query = lhspSendCommandNoticeDao.getSession().createSQLQuery(sql.toString());
			List list = query.list();
			if(list != null && list.size() > 0){
				returnStr =  (String)list.get(0);
			}
		}
		return returnStr;
	}
	
	/**
	 * 根据业务ID和业务编码获取办理结果
	 * @param acceptNo
	 * @param templateCode
	 * @return
	 */
	public String getBljgByAcceptNoAndTemplateCode(String masterEntityKey,String templateCode) throws Exception{
		String returnStr = "";
		if("csdlwj".equals(templateCode) || "csdllszysp".equals(templateCode) || templateCode.contains("jzfqwczzxn_") || templateCode.contains("jzfqwczzpf_")){
			if("csdlwj".equals(templateCode) || "csdllszysp".equals(templateCode)){
				Map<String,String> bljgAndBljgmsMap = WfUtilsEX.getBljgAndBljgms(templateCode, masterEntityKey, ywPrintDocService);
				if(bljgAndBljgmsMap != null){
					if( "6".equals(bljgAndBljgmsMap.get("bljg"))){
						returnStr = "不同意";
					}else if( "0".equals(bljgAndBljgmsMap.get("bljg"))){
						returnStr = "同意";
					}else if( "5".equals(bljgAndBljgmsMap.get("bljg"))){
						returnStr = "不一致";
					}
				}
			}else if(templateCode.contains("jzfqwczzxn_") || templateCode.contains("jzfqwczzpf_")){
				StringBuffer sql = new StringBuffer();
				sql.append("select t.sftyff from YW_YN_JZFQWCZC_CS t,");
				if(templateCode.contains("jzfqwczzxn_")){
					sql.append(" YW_YN_JZFQWCZC_XN a ");
				}else if(templateCode.contains("jzfqwczzpf_")){
					sql.append(" YW_YN_JZFQWCZC_PFSN a ");
				}
				sql.append(" where a.csid=t.id and a.id='"+masterEntityKey+"'");
				SQLQuery query = lhspSendCommandNoticeDao.getSession().createSQLQuery(sql.toString());
				List list = query.list();
				if(list != null && list.size() > 0){
					String sftyff = (String)list.get(0);
					if("2".equals(sftyff)){
						returnStr = "不同意";
					}else{
						returnStr = "同意";
					}				
				}
			}
		}
		return returnStr;
	}
	
	
	@Override
	public void delete(Long... ids) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public LhspUpdateFlowStateForm get(Long id) {
		LhspUpdateFlowState entity = lhspSendCommandNoticeDao.get(id);
		LhspUpdateFlowStateForm form = LhspUpdateFlowStateConvert.convertVoToForm(entity);
		return form;
	}
	@Override
	public void save(LhspUpdateFlowStateForm... forms) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void save(LhspUpdateFlowStateForm forms) {
		// TODO Auto-generated method stub
		
	}
	public void save(LhspUpdateFlowState lhspUpdateFlowState){
		lhspSendCommandNoticeDao.save(lhspUpdateFlowState);
	}

}
